package pers.vic.boot.util;

import java.io.BufferedReader;
import java.io.File;
import java.io.FilenameFilter;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 描述:Typro 文件夹copy后 对应图片copy
 * @author Vic.xu
 * @date 2021-12-29 10:10
 */
public class TyproImageTransfer {

    private static final String IMAGE_REGEX = "]\\(assert/(.*?)\\)";
    private static Pattern pattern = Pattern.compile(IMAGE_REGEX, Pattern.CASE_INSENSITIVE | Pattern.DOTALL);


    public static void main(String[] args) throws IOException {

    }

    public static void processPath() throws IOException {
        String markdownPath = "E:\\document\\jianguoyun\\我的坚果云\\typora\\004-学习经历\\002-自考\\For-202104\\计算机系统结构02325";
        String sourceImagePath = "E:\\document\\jianguoyun\\我的坚果云\\typora\\004-学习经历\\002-自考\\assert";
        String targetImagePath = "E:\\document\\jianguoyun\\我的坚果云\\typora\\004-学习经历\\002-自考\\For-202104\\计算机系统结构02325\\assert";
        transferAll(markdownPath, sourceImagePath, targetImagePath);
    }

    public static void process() throws IOException {
        String markdownFile = "E:\\document\\jianguoyun\\我的坚果云\\typora\\004-学习经历\\002-自考\\For-202104\\计算机网络原理04741\\002-计算机网络原理.md";
        String sourceImagePath = "E:\\document\\jianguoyun\\我的坚果云\\typora\\004-学习经历\\002-自考\\assert";
        String targetImagePath = "E:\\document\\jianguoyun\\我的坚果云\\typora\\004-学习经历\\002-自考\\For-202104\\计算机网络原理04741\\assert";
        transfer(markdownFile, sourceImagePath, targetImagePath);
    }

    public static boolean move(String sourcePath, String targetPath, String fileName) throws IOException {
        Path path = Paths.get(sourcePath, fileName);
        if (Files.notExists(path)) {
            System.out.println(path + " notExists!");
            return false;
        }
        Files.move(Paths.get(sourcePath, fileName), Paths.get(targetPath, fileName));
        return true;
    }

    /**
     * 文件夹下所有markdown里的图片转移
     * @param markdownPath  markdown 文件
     * @param sourceImagePath 原图片文件件
     * @param targetImagePath 目标图片文件夹
     */
    public static void transferAll(String markdownPath, String sourceImagePath, String targetImagePath) throws IOException {
        new File(markdownPath).listFiles(new FilenameFilter() {
            @Override
            public boolean accept(File dir, String name) {
                if (name.endsWith(".md")) {
                    try {
                        transfer(markdownPath + File.separator + name, sourceImagePath, targetImagePath);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                return false;
            }
        });
    }

    /**
     * 单个markdown里的图片转移
     * @param markdownFile  markdown 文件
     * @param sourceImagePath 原图片文件件
     * @param targetImagePath 目标图片文件夹
     */
    public static void transfer(String markdownFile, String sourceImagePath, String targetImagePath) throws IOException {
        List<String> imageNameList = fetchImages(markdownFile);
        System.err.println(markdownFile + " 文件包含的图片数为：" + imageNameList.size());
        imageNameList.forEach(name -> {
            try {
                boolean success = move(sourceImagePath, targetImagePath, name);
                if (!success) {
                    System.err.println("move " + name + " failed");
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        });
        System.out.println("transfer over");
    }


    public static List<String> fetchImages(String markdownFile) throws IOException {
        List<String> imageNameList = new ArrayList<>();
        Path path = Paths.get(markdownFile);
        BufferedReader reader = Files.newBufferedReader(path, StandardCharsets.UTF_8);
        String line = null;
        while ((line = reader.readLine()) != null) {
            String name = getImageName(line);
            if (name != null) {
                imageNameList.add(name);
            }
        }
        return imageNameList;
    }

    public static String getImageName(String line) {
        Matcher matcher = pattern.matcher(line);
        while (matcher.find()) {
            return matcher.group(1).trim();
        }
        return null;
    }
}
